<?php
namespace Admin\Controller;
class NewsController extends BaseController {


	public function index(){
		$news=M('new');
		$total = $news->count();
		$Page = new \Think\Page($total, 10);
		// $data=$new->order('add_time desc')->limit($Page->firstRow, $Page->listRows)->select();
		$data=$news->select();

		$this->assign('page', $Page->show());
		$this->assign('data', $data);
		$this->display();
	}
	
	public function add(){
		
		$this->display('News/add');
	}
	
	public function doadd(){
		
		 $imgname=$this->upload();
		 $data = I();
		// $_POST['thumb']=$imgname[0];
		// $_POST['add_time']=time();
		$news=M('new');
		// var_dump($data);exit;
		if($news->data($data)->add()){
			$this->success('添加成功',U('News/index'));
		}else{
			$this->error('添加失败');
		}
	}
	
	public function edit(){
	      $news=M('new');
		$id=I('id');
		$this->info=$news->find($id);
		
		$this->display();
	}

	public function doedit(){
	//	dump($_FILES[thumb][name]);die;
		if(!empty($_FILES[thumb][name])){
			$imgname=$this->upload();//p($imgname);die;
			$_POST['thumb']=$imgname[0];
		}
		$news=M('new');
		$_POST['add_time']=time();
		if($news->save($_POST)){
			$this->success('更新成功',U('News/index'));
		}else{
			$this->error('更新失败');
		}

	}

	
        
        public function del()
    {
        $news=D('new');
        $id=I('id');
        if($news->delete($id))
        {
            $this->success('成功删除栏目！',U('index'));
        }else
        {
            $this->error('删除栏目失败！');
        }
    
    }
	
	
	
	public function upload(){
		
	
		//图片上传处理
		if(!empty($_FILES)){
			
			$config = array(
					'savePath'   =>    '/news/',
					'saveName'   =>    array('uniqid',''),
					'exts'       =>    array('jpg', 'gif', 'png'),
					'autoSub'    =>    true,
					'subName'    =>    array('date','Y-m'),
			);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();
			if(!$info)  {
				//$this->error($upload->getError()); // 上传错误提示错误信息
			}  else{
				foreach($info as $file){
					$filename[]= $file['savepath'].$file['savename'];
					// echo 11;
					// echo $filename;
				}
				return $filename;
			}
		}
	}
	
}